<?php
/** 
*
* @package acp
* @version $Id: $
* @copyright (c) 2008 Poppertom69
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*
*/
if (!defined('IN_PHPBB'))
{
	exit;
}

/**
* @package acp
*/
class acp_post_bot
{
	var $u_action, $wpm;

	function main($id, $mode)
	{
		global $db, $user, $template, $cache;
		global $config, $phpbb_root_path, $phpEx, $post_bot;

		$user->add_lang('posting');
		$user->add_lang('mods/post_bot');

		include ($phpbb_root_path . 'includes/functions_user.' . $phpEx);
		include ($phpbb_root_path . 'includes/functions_display.' . $phpEx);
		include ($phpbb_root_path . 'includes/message_parser.' . $phpEx);

		$mode		= request_var('mode', 'settings');
		$submit		= (isset($_POST['submit']))  ? true : false;
		$preview	= (isset($_POST['preview'])) ? true : false;
		
		$form_key = 'acp_post_bot';
		add_form_key($form_key);
		
		if ($submit && !check_form_key($form_key))
		{
			trigger_error($user->lang['FORM_INVALID'] . adm_back_link($this->u_action));
		}
		
		switch ($mode)
		{
			case 'settings':
				$pb_available_extensions = pb::available_extensions();
				foreach ($pb_available_extensions as $value)
				{
					$template->assign_block_vars('pb_extensions', array(
						'S_ENABLED'	=> pb::check_extension_status($value[0]),
						'NAME'		=> $value[0],
						'LABEL'		=> $user->lang[strtoupper($value[0])],
						'EXPLAIN'	=> $user->lang[strtoupper($value[0]) . '_EXPLAIN'],
					));
				}			
				
				$nor_subj	= '';
				$nor_msg	= '';
				
				if ($submit)
				{
					foreach ($pb_available_extensions as $value)
					{
						$extension_value = request_var($value[0], 1);
						
						if ($extension_value != $config[$value[0]])
						{
							set_config($value[0], $extension_value);
							if ($extension_value == 1)
							{
								pb::install_post_bot_extension($value[0]);
							}
						}
					}
					
					add_log('admin', 'LOG_PB_SETTINGS_UPDATED');
					trigger_error($user->lang['PB_UPDATED'] . adm_back_link($this->u_action));
				}
				
				$tpl = 'acp_post_bot_settings';
			break;			
				
			case 'welcome':
				$sql = 'SELECT username
					FROM ' . USERS_TABLE . '
					WHERE user_id = ' . $post_bot['welcome_bot_id'];
				$result = $db->sql_query($sql);
				$username = (string) $db->sql_fetchfield('username');
				$db->sql_freeresult($result);
			
				$template->assign_vars(array(
					'ERROR'			=> !pb::check_extension_status('post_bot_welcome_user'),
					'ERROR_MSG'		=> $user->lang['POST_BOT_WELCOME_USER_NA'],
					'FORUM_BOX'		=> show_forums($post_bot['welcome_forum']),
					'USER'			=> $username,
					'S_SUBJECT'		=> true,
				));				
				
				$nor_subj	= $post_bot['welcome_subject_1'];
				$nor_msg	= $post_bot['welcome_message_1'];
				
				if ($submit)
				{
					$username 	= request_var('username', '');
					$forum_id 	= request_var('post_forum', 1);
					$subject	= request_var('subject', '');
					$message	= request_var('message', '');
				
					$sql = 'SELECT user_id
						FROM ' . USERS_TABLE . "
						WHERE username_clean = '" . $db->sql_escape(utf8_clean_string($username)) . "'";
					$result = $db->sql_query($sql);
					$user_id = (int) $db->sql_fetchfield('user_id');
					$db->sql_freeresult($result);
					
					if (!$user_id)
					{
						trigger_error($user->lang['NO_USER'] . adm_back_link($this->u_action), E_USER_WARNING);
					}
					
					pb::set_post_bot('welcome_bot_id', $user_id);
					pb::set_post_bot('welcome_forum', $forum_id);
					pb::set_post_bot('welcome_subject_1', $subject);
					pb::set_post_bot('welcome_message_1', $message);
					
					add_log('admin', 'LOG_PB_WELCOME_UPDATED');
					trigger_error($user->lang['PB_UPDATED'] . adm_back_link($this->u_action));
				}
			
				$tpl = 'acp_post_bot';
			break;			
				
			case 'birthday':
				$sql = 'SELECT username
					FROM ' . USERS_TABLE . '
					WHERE user_id = ' . $post_bot['birthday_bot_id'];
				$result = $db->sql_query($sql);
				$username = (string) $db->sql_fetchfield('username');
				$db->sql_freeresult($result);
			
				$template->assign_vars(array(
					'ERROR'			=> !pb::check_extension_status('post_bot_users_birthday'),
					'ERROR_MSG'		=> $user->lang['POST_BOT_BIRTHDAY_NA'],
					'FORUM_BOX'		=> show_forums($post_bot['birthday_forum']),
					'USER'			=> $username,
					'S_SUBJECT'		=> true,
				));
				
				$nor_subj	= $post_bot['birthday_subject_1'];
				$nor_msg	= $post_bot['birthday_message_1'];
				
				if ($submit)
				{
					$username 	= request_var('username', '');
					$forum_id 	= request_var('post_forum', 1);
					$subject	= request_var('subject', '');
					$message	= request_var('message', '');
				
					$sql = 'SELECT user_id
						FROM ' . USERS_TABLE . "
						WHERE username_clean = '" . $db->sql_escape(utf8_clean_string($username)) . "'";
					$result = $db->sql_query($sql);
					$user_id = (int) $db->sql_fetchfield('user_id');
					$db->sql_freeresult($result);
					
					if (!$user_id)
					{
						trigger_error($user->lang['NO_USER'] . adm_back_link($this->u_action), E_USER_WARNING);
					}
					
					pb::set_post_bot('birthday_bot_id', $user_id);
					pb::set_post_bot('birthday_forum', $forum_id);
					pb::set_post_bot('birthday_subject_1', $subject);
					pb::set_post_bot('birthday_message_1', $message);
					
					add_log('admin', 'LOG_PB_BIRTHDAY_UPDATED');
					trigger_error($user->lang['PB_UPDATED'] . adm_back_link($this->u_action));
				}
			
				$tpl = 'acp_post_bot';
			break;
			
			case 'auto_reply':
				$sql = 'SELECT username
					FROM ' . USERS_TABLE . '
					WHERE user_id = ' . $post_bot['auto_reply_bot_id'];
				$result = $db->sql_query($sql);
				$username = (string) $db->sql_fetchfield('username');
				$db->sql_freeresult($result);
			
				$template->assign_vars(array(
					'ERROR'			=> !pb::check_extension_status('post_bot_auto_reply'),
					'ERROR_MSG'		=> $user->lang['POST_BOT_AUTO_REPLY_NA'],
					'FORUM_BOX'		=> show_forums($post_bot['auto_reply_forums']),
					'USER'			=> $username,
					'S_SUBJECT'		=> false,
				));
				
				$nor_subj	= '';
				$nor_msg	= $post_bot['auto_reply_message_2'];
				
				if ($submit)
				{
					$username 	= request_var('username', '');
					$forum_id 	= request_var('post_forum', 1);
					$message	= request_var('message', '');
				
					$sql = 'SELECT user_id
						FROM ' . USERS_TABLE . "
						WHERE username_clean = '" . $db->sql_escape(utf8_clean_string($username)) . "'";
					$result = $db->sql_query($sql);
					$user_id = (int) $db->sql_fetchfield('user_id');
					$db->sql_freeresult($result);
					
					if (!$user_id)
					{
						trigger_error($user->lang['NO_USER'] . adm_back_link($this->u_action), E_USER_WARNING);
					}
					
					pb::set_post_bot('auto_reply_bot_id', $user_id);
					pb::set_post_bot('auto_reply_forums', $forum_id);
					pb::set_post_bot('auto_reply_message_2', $message);
					
					add_log('admin', 'LOG_PB_AUTO_REPLY_UPDATED');
					trigger_error($user->lang['PB_UPDATED'] . adm_back_link($this->u_action));
				}
			
				$tpl = 'acp_post_bot';
			break;
		}

		//Set data for vars
		$data = array(
			'username'	=> $user->data['username'],
			'user_id'	=> $user->data['user_id'],
			'age'		=> '18',
			'subject'	=> 'Please help',
		);		
		
		//Correctly assign variables.
		$vars = pb::get_vars($mode, $data);

		if ($preview == true)
		{
			$pre_subject	= utf8_normalize_nfc(request_var('subject', ''));
			$pre_message	= utf8_normalize_nfc(request_var('message', ''));
		
			$pre_subj		= str_replace(array_keys($vars), array_values($vars), $pre_subject);
			$pre_msg		= str_replace(array_keys($vars), array_values($vars), $pre_message);

			$uid			= $bitfield			= $options		= '';
			$allow_bbcode	= $allow_smilies	= $allow_urls	= true;

			generate_text_for_storage($pre_msg, $uid, $bitfield, $options, $allow_bbcode, $allow_urls, $allow_smilies);

			$preview_msg	= generate_text_for_display($pre_msg, $uid, $bitfield, $options);
			$preview_subj	= $pre_subj;
		}	
				
		foreach ($vars as $var => $example) 
		{
			$name = str_replace('{', '', $var);
			$name = str_replace('}', '', $name);
			
			$template->assign_block_vars('vars', array(
				'NAME'		=> $user->lang['PB_' . $name],
				'VARIABLE'	=> $var,
				'EXAMPLE'	=> $example)
			);
		}

		display_custom_bbcodes();

		$template->assign_vars(array(
			'PREVIEW_MSG'		=> ($preview) ? $preview_msg : '',
			'PREVIEW_SUBJ'		=> ($preview) ? $preview_subj : '',
			
			'MESSAGE'			=> ($preview) ? $pre_message : $nor_msg,
			'SUBJECT'			=> ($preview) ? $pre_subject : $nor_subj,

			'L_SMILIES'			=> strtoupper($user->lang['SMILIES']),

			'S_BBCODE_ALLOWED'	=> true,
			'S_BBCODE_QUOTE'	=> true,
			'S_BBCODE_IMG'		=> true,
			'S_LINKS_ALLOWED'	=> true,
			'S_BBCODE_FLASH'	=> true,
			'S_PREVIEW'			=> $preview,

			'T_TEMPLATE_PATH'	=> $phpbb_root_path . 'styles/prosilver/template',
			'U_SMILIES'			=> append_sid("{$phpbb_root_path}posting.$phpEx", 'mode=smilies'),
			'U_ACTION'			=> $this->u_action,
			'U_FIND_USER'		=> append_sid("{$phpbb_root_path}memberlist.$phpEx", 'mode=searchuser&amp;form=postform&amp;field=username&amp;select_single=true'),					
		));
		
		// Set up general vars
		$this->page_title	= 'ACP_PB_' . strtoupper($mode);
		$this->tpl_name		= $tpl;
	}	
}

/**
* Simple version of jumpbox, just lists authed forums
*/
function show_forums($select_id = false, $ignore_id = false, $ignore_acl = false, $ignore_nonpost = false, $ignore_emptycat = true, $only_acl_post = false, $return_array = false)
{
	global $db, $user, $auth;

	$acl = ($ignore_acl) ? '' : (($only_acl_post) ? 'f_post' : array('f_list', 'a_forum', 'a_forumadd', 'a_forumdel'));

	// This query is identical to the jumpbox one
	$sql = 'SELECT forum_id, forum_name, parent_id, forum_type, left_id, right_id
		FROM ' . FORUMS_TABLE . '
		ORDER BY left_id ASC';
	$result = $db->sql_query($sql, 600);

	$right = 0;
	$padding_store = array('0' => '');
	$padding = '';
	$forum_list = ($return_array) ? array() : '';

	// Sometimes it could happen that forums will be displayed here not be displayed within the index page
	// This is the result of forums not displayed at index, having list permissions and a parent of a forum with no permissions.
	// If this happens, the padding could be "broken"

	while ($row = $db->sql_fetchrow($result))
	{
		if ($row['left_id'] < $right)
		{
			$padding .= '&nbsp; &nbsp;';
			$padding_store[$row['parent_id']] = $padding;
		}
		else if ($row['left_id'] > $right + 1)
		{
			$padding = (isset($padding_store[$row['parent_id']])) ? $padding_store[$row['parent_id']] : '';
		}

		$right = $row['right_id'];
		$disabled = false;

		if ($acl && !$auth->acl_gets($acl, $row['forum_id']))
		{
			// List permission?
			if ($auth->acl_get('f_list', $row['forum_id']))
			{
				$disabled = true;
			}
			else
			{
				continue;
			}
		}

		if (
			((is_array($ignore_id) && in_array($row['forum_id'], $ignore_id)) || $row['forum_id'] == $ignore_id)
			||
			// Non-postable forum with no subforums, don't display
			($row['forum_type'] == FORUM_CAT && ($row['left_id'] + 1 == $row['right_id']) && $ignore_emptycat)
			||
			($row['forum_type'] != FORUM_POST && $ignore_nonpost)
			)
		{
			$disabled = true;
		}
		if ($row['forum_type'] == FORUM_CAT)
		{
			$disabled = true;
		}

		if ($return_array)
		{
			// Include some more information...
			$selected = (is_array($select_id)) ? ((in_array($row['forum_id'], $select_id)) ? true : false) : (($row['forum_id'] == $select_id) ? true : false);
			$forum_list[$row['forum_id']] = array_merge(array('padding' => $padding, 'selected' => ($selected && !$disabled), 'disabled' => $disabled), $row);
		}
		else
		{
			$selected = (is_array($select_id)) ? ((in_array($row['forum_id'], $select_id)) ? ' selected="selected"' : '') : (($row['forum_id'] == $select_id) ? ' selected="selected"' : '');
			$forum_list .= '<option value="' . $row['forum_id'] . '"' . (($disabled) ? ' disabled="disabled" class="disabled-option"' : $selected) . '>' . $padding . $row['forum_name'] . '</option>';
		}
	}
	$db->sql_freeresult($result);
	unset($padding_store);

	return $forum_list;
}
?>